/**
 *  提示与加载工具类
 */
export default class Tips {
  constructor() {
    this.isLoading = false;
    this.pause = false;
  }

  /**
   * 成功提示框
   * @param {*} title
   * @param {*} duration
   */
  static success(title, duration = 1000) {
    wx.showToast({
      title: title,
      icon: 'success',
      mask: true,
      duration: duration
    });
    if (duration > 0) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve();
        }, duration);
      });
    }
  }

  /**
   * 常规alert弹框
   * @param {*} text
   * @param {*} title
   */
  static modal(text, title = '提示') {
    return new Promise((resolve, reject) => {
      wx.showModal({
        title: title,
        content: text,
        showCancel: false,
        success: res => {
          resolve(res);
        },
        fail: res => {
          reject(res);
        }
      });
    });
  }

  /**
   * 带确认框的Promise类型弹框
   * @param {*} text
   * @param {*} callBack
   * @param {*} payload
   * @param {*} title
   */
  static confirm(text, callBack, payload = {}, title = '提示') {
    console.log('tipssss');
    return new Promise((resolve, reject) => {
      wx.showModal({
        title: title,
        content: text,
        showCancel: true,
        success: res => {
          if (res.confirm) {
            resolve(payload);
          } else if (res.cancel) {
            reject(payload);
          }
          callBack && callBack(res.confirm);
        },
        fail: res => {
          reject(payload);
        }
      });
    });
  }

  /**
   * 带图片的提示框
   * @param {*} title
   * @param {*} image
   */
  static alert(title, image) {
    wx.showToast({
      title: title,
      image: image,
      mask: true,
      duration: 500
    });
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve();
      }, 500);
    });
  }

  /**
   * 错误类型 toast弹框
   * @param {*} title
   * @param {*} onHide
   */
  static error(title, onHide) {
    wx.showToast({
      title: title,
      icon: 'none',
      mask: true,
      duration: 1000
    });
    if (onHide) {
      setTimeout(() => {
        onHide();
      }, 500);
    }
  }

  /**
   * 常规加载框
   * @param {*} title
   */
  static loading(title = '加载中') {
    if (Tips.isLoading) {
      return;
    }
    Tips.isLoading = true;
    if (wx.showLoading) {
      wx.showLoading({
        title: title,
        mask: true
      });
    }
  }

  /**
   * 导航栏loading
   */
  static navLoading() {
    if (Tips.isLoading) {
      return;
    }
    Tips.isLoading = true;
    wx.showNavigationBarLoading();
  }

  /**
   * 取消loading状态
   */
  static loaded() {
    if (Tips.isLoading) {
      Tips.isLoading = false;
      if (wx.hideLoading) {
        wx.hideLoading();
        wx.hideNavigationBarLoading();
      }
    }
  }

  /**
   * 常规的actionSheet
   * @param {*} items
   */
  static action(...items) {
    return new Promise((resolve, reject) => {
      wx.showActionSheet({
        itemList: items,
        itemColor: '#A53334',
        success: res => {
          const result = {
            index: res.tapIndex,
            text: items[res.tapIndex]
          };
          resolve(result);
        },
        fail: res => {
          reject(res.errMsg);
        }
      });
    });
  }

  /**
   * 带回调函数的actionSheet
   * @param {*} items
   * @param {*} color
   * @param {*} functions
   */
  static actionWithFunc(items, color, ...functions) {
    wx.showActionSheet({
      itemList: items,
      itemColor: color,
      success: res => {
        const index = res.tapIndex;
        if (index >= 0 && index < functions.length) {
          functions[index]();
        }
      }
    });
  }

  static share(title, url, desc) {
    return {
      title: title,
      path: url,
      desc: desc,
      success: res => {
        Tips.toast('分享成功');
      }
    };
  }

  static setLoading() {
    Tips.isLoading = true;
  }
}
